// pages/OrganForm/index.js
const orgTypelist = [{
  typename: '组织'
}, {
  typename: '学生会'
}, {
  typename: '社团'
}, {
  typename: '创新创业项目'
}, {
  typename: '其它'
}]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentindex: 0,
    picCloudurl: [],
    orgdata: {},
    orgTypelist: orgTypelist,
    is_manage: false,
    edit: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getcurOrg(options.id)
    let is_manage = wx.getStorageSync('is_manage')
    if (is_manage === '') {
      let openid = wx.getStorageSync('OPENID')
      wx.cloud.database().collection('users').where({
        _openid: openid
      }).get({
        success: res => {
          is_manage = res.data[0].manage
          this.setData({
            is_manage: is_manage
          })
          wx.setStorageSync('is_manage', is_manage)
        }
      })
    } else {
      this.setData({
        is_manage: is_manage
      })
    }

  },
  getcurOrg: function (id) {
    wx.cloud.database().collection('orglist').where({
      _id: id
    }).get({
      success: (res) => {
        console.log(res)
        this.setData({
          orgdata: res.data[0]
        })
      }
    })
  },
  editOrgdetail: function () {
    let orgdata = this.data.orgdata
    orgdata.org_request = orgdata.org_request.join('\n')
    this.setData({
      edit: true,
      orgdata:orgdata
    })
  },
  delOrg: function () {
    let id = this.data.orgdata._id
    wx.showModal({
      title: '提示',
      content: '确定删除该组织信息吗？',
      showCancel: true,
      cancelText: '取消',
      confirmText: '确定',
      success: (result) => {
        if (result.confirm) {
          wx.cloud.database().collection('orglist').where({
            _id: id
          }).remove({
            success: res => {
              wx.showToast({
                title: '删除成功',
              })
              wx.navigateBack()
            }
          })
        }
      },
      fail: () => {
        return false
      },
      complete: () => {}
    })
  },
  addlabel:function (params) {
    let orgdata = this.data.orgdata
    orgdata.org_label.push('')
    this.setData({
      orgdata:orgdata
    })
  },
  bindPickerChange: function (e) {
    let index = e.detail.value
    let orgdata = this.data.orgdata
    orgdata.type = orgTypelist[index].typename
    this.setData({
      orgdata: orgdata,
    })
  },
  inputLocation: function (e) {
    let orgdata = this.data.orgdata
    orgdata.org_location = e.detail.value
    this.setData({
      orgdata:orgdata
    })
  },
  inputName: function (e) {
    let orgdata = this.data.orgdata
    orgdata.org_name = e.detail.value
    this.setData({
      orgdata:orgdata
    })
  },
  inputIntro: function (e) {
    let orgdata = this.data.orgdata
    orgdata.org_intro = e.detail.value
    this.setData({
      orgdata:orgdata
    })
  },
  inputRequest: function (e) {
    let orgdata = this.data.orgdata
    orgdata.org_request = e.detail.value
    this.setData({
      orgdata:orgdata
    })
  },
  //上传事件
  upImage(e) {
    let that = this
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        let picCloudurls = that.data.picCloudurl
        for (let i = 1; i <= tempFilePaths.length; i++) {
          wx.showLoading({
            title: '上传中' + i + '/' + tempFilePaths.length,
            mask: true
          })
          let filePath = tempFilePaths[i - 1];
          const name = Math.random() * 1000000;
          const cloudPath = '' + name + filePath.match(/\.[^.]+?$/)[0]
          wx.cloud.uploadFile({
            cloudPath: 'orgImgs/' + cloudPath,
            filePath: filePath,
            name: 'file',
            formData: {},
            success(res) {
              wx.hideLoading();
              picCloudurls.push(res.fileID)
              that.setData({
                picCloudurl: picCloudurls
              })
            }
          })
        }

      }
    })
  },
  inputLabel:function (e) {
    let index = e.currentTarget.dataset.index
    let orgdata = this.data.orgdata
    orgdata.org_label[index] = e.detail.value
    this.setData({
      orgdata:orgdata
    })
  },
  formSubmit: function (e) {
    let data = e.detail.value
    console.log(data)
    let orgdata=this.data.orgdata
    let id = orgdata._id
    delete orgdata['_id']
    delete orgdata['_openid']
    orgdata.org_request = data.org_request.split('\n')
    orgdata.imgUrls = orgdata.imgUrls.concat(this.data.picCloudurl)

    wx.cloud.database().collection('orglist').doc(id).update({
      data:orgdata,
      success:res=>{
        wx.showToast({
          title: '数据更新成功',
        })
        this.setData({
          edit: false
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})